<?php
	/**
	 * 物品资产控制器
	 * 作者：莫凯
	 * 日期：2014/1/6
	 */
class GoodsAction extends MessageAction {
		/*添加领取物品流程*/
		public function addGetGoods(){
			$p=M('Process');
			$goods=M('Goods');
	        $number=$_POST['process_num'];
	        $number=intval(substr($number, 0,10));
	        $result=$p->where("process_num='".$number."' and process_state=1")->find();//已经申请
	        if($result){
	            echo '该申请已提交！';
	        }
	        else{
	        	$result_second=$p->where("process_num='".$number."' and process_state=0")->find();//如果已经保存了
	            if($result_second){
	                $dataProcess['process_state']=1;
	                $data['goods_state']=1;
	                $p->where("process_num='".$number."'")->save($dataProcess);
	                $field=$p->where("process_num='".$number."'")->field('process_id,process_begin_id,process_now_id,process_title')->find();
	                $goods->where("goods_process_id='".$field['process_id']."'")->save($data);

	                $msg_from_name=$this->getOneEmployeName($field['process_begin_id']);
	                $msg_title=$field['process_title'];
	                $msg_content=$msg_title.$msg_from_name.'向您提交了一条申请，请注意查收';
	                if(I('msg_tip')){
	                    //发送消息
	                    $this->sendMsg($field['process_now_id'],$field['process_begin_id'],$msg_title,$msg_content);
	                }

	                if(I('msg_phone')){
	                    //发送短信
	                    $mobile=I('phone_num');
	                    $content=$msg_content."【来自丽丰OA系统】";
	                    $mobileids=$mobile.strval(time());
	                    $this->sendSMS($mobile,$content,$mobileids);
	                }
	                
	                echo '申请成功！';
	            }
	            else{

					$data['goods_number']="A1".time();
					$data['goods_type']="1";
					$data['goods_employe_id']=$_POST['employeId'];
					$data['goods_des']=$_POST['des'];
					$data['goods_tag']=$_POST['tag'];
					/*流程数据*/
					$process["process_num"]=$number;
					$process['process_title']="物品领用审批单";
					$process['process_begin_id']=$data['goods_employe_id'];
					$process['process_now_id']=$_POST['check_person_id'];
					$process['process_time']=$process["process_num"];
					$process['process_state']=$data['goods_tag'];
					$process['process_type']='Goods';
					$process['process_now_id_if_approval']=0;
					$process['process_parent_id']=0;
					$m=M("process");
					$id=$m->add($process);
					if(false==$id){
						echo 0;
						exit;
					}else{
						$data['goods_process_id']=$id;
						$m=M("goods");
						$id1=$m->add($data);
						if(false==$id1){
							echo 0;
							exit;
						}else{
							$m=M('getgoods');
							$goodsList=$_POST['data'];
							foreach ($goodsList['rows'] as $key=>$val){
								$arr=array(
									'getgoods_name'=>$val['name'],
									'getgoods_count'=>$val['count'],
									'getgoods_use'=>$val['use'],
									'getgoods_goods_id'=>$id1
								);
								$id2=$m->add($arr);
								if(false==$id2){
									echo 0;
									exit;
								}
							}
						}
					}

					$msg_from_name=$this->getOneEmployeName($data['goods_employe_id']);
		            $msg_content=$msg_from_name.'向您提交了一条物品领用审批单，请注意查收';
		            if(I('msg_tip')){
		                //发送消息
		                $msg_title="物品领用审批单";
		                $this->sendMsg($_POST['check_person_id'],$data['goods_employe_id'],$msg_title,$msg_content);
		            }

		            if(I('msg_phone')){
		                //发送短信
		                $mobile=I('phone_num');
		                $content=$msg_content."【来自丽丰OA系统】";
		                $mobileids=$mobile.strval(time());
		                $this->sendSMS($mobile,$content,$mobileids);
		            }

					echo '申请成功！';
				}
			}
		}


		/**
	     * [saveGetGoodsPost description]保存请假条
	     * @return [type] [description]
	     */
	    public function saveGetGoodsPost(){
	    	$p=M('Process');
	        $number=$_POST['process_num'];
	        $number=intval(substr($number, 0,10));
	        $result=$p->where("process_num='".$number."'")->find();//已经申请
	        if($result){
	            echo '该申请已保存！';
	        }
	        else{
				$data['goods_number']="A1".time();
				$data['goods_type']="1";
				$data['goods_employe_id']=$_POST['employeId'];
				$data['goods_des']=$_POST['des'];
				$data['goods_tag']=$_POST['tag'];
				/*流程数据*/
				$process["process_num"]=$number;
				$process['process_title']="物品领用审批单";
				$process['process_begin_id']=$data['goods_employe_id'];
				$process['process_now_id']=$_POST['check_person_id'];
				$process['process_time']=$process["process_num"];
				$process['process_state']=$data['goods_tag'];
				$process['process_type']='Goods';
				$process['process_now_id_if_approval']=0;
				$process['process_parent_id']=0;
				$m=M("process");
				$id=$m->add($process);
				if(false==$id){
					echo 0;
					exit;
				}else{
					$data['goods_process_id']=$id;
					$m=M("goods");
					$id1=$m->add($data);
					if(false==$id1){
						echo 0;
						exit;
					}else{
						$m=M('getgoods');
						$goodsList=$_POST['data'];
						foreach ($goodsList['rows'] as $key=>$val){
							$arr=array(
								'getgoods_name'=>$val['name'],
								'getgoods_count'=>$val['count'],
								'getgoods_use'=>$val['use'],
								'getgoods_goods_id'=>$id1
							);
							$id2=$m->add($arr);
							if(false==$id2){
								echo 0;
								exit;
							}
						}
					}
				}
				echo '保存成功！';
			}
	        
	    }
		
		/*添加购买物品流程*/
		public function addBuyGoods(){
			$p=M('Process');
			$goods=M('Goods');
	        $number=$_POST['process_num'];
	        $number=intval(substr($number, 0,10));
	        $result=$p->where("process_num='".$number."' and process_state=1")->find();//已经申请
	        if($result){
	            echo '该申请已提交！';
	        }
	        else{
	        	$result_second=$p->where("process_num='".$number."' and process_state=0")->find();//如果已经保存了
	            if($result_second){
	                $dataProcess['process_state']=1;
	                $data['goods_state']=1;
	                $p->where("process_num='".$number."'")->save($dataProcess);
	                $field=$p->where("process_num='".$number."'")->field('process_id,process_begin_id,process_now_id,process_title')->find();
	                $goods->where("goods_process_id='".$field['process_id']."'")->save($data);

	                $msg_from_name=$this->getOneEmployeName($field['process_begin_id']);
	                $msg_title=$field['process_title'];
	                $msg_content=$msg_title.$msg_from_name.'向您提交了一条申请，请注意查收';
	                if(I('msg_tip')){
	                    //发送消息
	                    $this->sendMsg($field['process_now_id'],$field['process_begin_id'],$msg_title,$msg_content);
	                }

	                if(I('msg_phone')){
	                    //发送短信
	                    $mobile=I('phone_num');
	                    $content=$msg_content."【来自丽丰OA系统】";
	                    $mobileids=$mobile.strval(time());
	                    $this->sendSMS($mobile,$content,$mobileids);
	                }
	                
	                echo '申请成功！';
	            }
	            else{

					$data['goods_number']="A2".time();
					$data['goods_type']="2";
					$data['goods_employe_id']=$_POST['employeId'];
					$data['goods_des']=$_POST['des'];
					$data['goods_tag']=$_POST['tag'];
					/*流程数据*/
					$process["process_num"]=$number;
					$process['process_title']="物品购买申请单";
					$process['process_begin_id']=$data['goods_employe_id'];
					$process['process_now_id']=$_POST['check_person_id'];
					$process['process_time']=$process["process_num"];
					$process['process_state']=$data['goods_tag'];
					$process['process_type']='Goods';
					$process['process_now_id_if_approval']=0;
					$process['process_parent_id']=0;
					$m=M("process");
					$id=$m->add($process);
					if(false==$id){
						echo 0;
						exit;
					}else{
						$data['goods_process_id']=$id;
						$m=M("goods");
						$id1=$m->add($data);
						if(false==$id1){
							echo 0;
							exit;
						}else{
							$m=M('buygoods');
							$goodsList=$_POST['data'];
							foreach ($goodsList['rows'] as $key=>$val){
								$arr=array(
									'buygoods_name'=>$val['name'],
									'buygoods_count'=>$val['count'],
									'buygoods_price'=>$val['price'],
									'buygoods_use'=>$val['use'],
									'buygoods_goods_id'=>$id1
								);
								$id2=$m->add($arr);
								if(false==$id2){
									echo 0;
									exit;
								}
							}
						}
					}

					$msg_from_name=$this->getOneEmployeName($data['goods_employe_id']);
		            $msg_content=$msg_from_name.'向您提交了一条购物申请单，请注意查收';
		            if(I('msg_tip')){
		                //发送消息
		                $msg_title="购物申请单";
		                $this->sendMsg($_POST['check_person_id'],$data['goods_employe_id'],$msg_title,$msg_content);
		            }

		            if(I('msg_phone')){
		                //发送短信
		                $mobile=I('phone_num');
		                $content=$msg_content."【来自丽丰OA系统】";
		                $mobileids=$mobile.strval(time());
		                $this->sendSMS($mobile,$content,$mobileids);
		            }

					echo '申请成功！';
				}
			}

		}

		public function saveBuyGoodsPost(){
			$p=M('Process');
	        $number=$_POST['process_num'];
	        $number=intval(substr($number, 0,10));
	        $result=$p->where("process_num='".$number."'")->find();//已经申请
	        if($result){
	            echo '该申请已保存！';
	        }
	        else{
				$data['goods_number']="A2".time();
				$data['goods_type']="2";
				$data['goods_employe_id']=$_POST['employeId'];
				$data['goods_des']=$_POST['des'];
				$data['goods_tag']=$_POST['tag'];
				/*流程数据*/
				$process["process_num"]=$number;
				$process['process_title']="物品购买申请单";
				$process['process_begin_id']=$data['goods_employe_id'];
				$process['process_now_id']=$_POST['check_person_id'];
				$process['process_time']=$process["process_num"];
				$process['process_state']=$data['goods_tag'];
				$process['process_type']='Goods';
				$process['process_now_id_if_approval']=0;
				$process['process_parent_id']=0;
				$m=M("process");
				$id=$m->add($process);
				if(false==$id){
					echo 0;
					exit;
				}else{
					$data['goods_process_id']=$id;
					$m=M("goods");
					$id1=$m->add($data);
					if(false==$id1){
						echo 0;
						exit;
					}else{
						$m=M('buygoods');
						$goodsList=$_POST['data'];
						foreach ($goodsList['rows'] as $key=>$val){
							$arr=array(
								'buygoods_name'=>$val['name'],
								'buygoods_count'=>$val['count'],
								'buygoods_price'=>$val['price'],
								'buygoods_use'=>$val['use'],
								'buygoods_goods_id'=>$id1
							);
							$id2=$m->add($arr);
							if(false==$id2){
								echo 0;
								exit;
							}
						}
					}
				}
				echo '保存成功！';
			}
		}

		/*添加设备维修流程*/
		public function addRepair(){
			$p=M('Process');
			$goods=M('Goods');
	        $number=$_POST['process_num'];
	        $number=intval(substr($number, 0,10));
	        $result=$p->where("process_num='".$number."' and process_state=1")->find();//已经申请
	        if($result){
	            echo '该申请已提交！';
	        }
	        else{
	        	$result_second=$p->where("process_num='".$number."' and process_state=0")->find();//如果已经保存了
	            if($result_second){
	                $dataProcess['process_state']=1;
	                $data['goods_state']=1;
	                $p->where("process_num='".$number."'")->save($dataProcess);
	                $field=$p->where("process_num='".$number."'")->field('process_id,process_begin_id,process_now_id,process_title')->find();
	                $goods->where("goods_process_id='".$field['process_id']."'")->save($data);

	                $msg_from_name=$this->getOneEmployeName($field['process_begin_id']);
	                $msg_title=$field['process_title'];
	                $msg_content=$msg_title.$msg_from_name.'向您提交了一条申请，请注意查收';
	                if(I('msg_tip')){
	                    //发送消息
	                    $this->sendMsg($field['process_now_id'],$field['process_begin_id'],$msg_title,$msg_content);
	                }

	                if(I('msg_phone')){
	                    //发送短信
	                    $mobile=I('phone_num');
	                    $content=$msg_content."【来自丽丰OA系统】";
	                    $mobileids=$mobile.strval(time());
	                    $this->sendSMS($mobile,$content,$mobileids);
	                }
	                
	                echo '申请成功！';
	            }
	            else{

					$data['goods_number']="A3".time();
					$data['goods_type']="3";
					$data['goods_employe_id']=$_POST['employeId'];
					$data['goods_des']=$_POST['des'];
					$data['goods_tag']=$_POST['tag'];
					/*流程数据*/
					$process["process_num"]=$number;
					$process['process_title']="设施(设备)维修申请单";
					$process['process_begin_id']=$data['goods_employe_id'];
					$process['process_now_id']=$_POST['check_person_id'];
					$process['process_time']=$process["process_num"];
					$process['process_state']=$data['goods_tag'];
					$process['process_type']='Goods';
					$process['process_now_id_if_approval']=0;
					$process['process_parent_id']=0;
					$m=M("process");
					$id=$m->add($process);
					if(false==$id){
						echo 0;
						exit;
					}else{
						$data['goods_process_id']=$id;
						$m=M("goods");
						$id1=$m->add($data);
						if(false==$id1){
							echo 0;
							exit;
						}else{
							$m=M('repair');
							$goodsList=$_POST['data'];
							foreach ($goodsList['rows'] as $key=>$val){
								$arr=array(
									'repair_name'=>$val['name'],
									'repair_count'=>$val['count'],
									'repair_specifications'=>$val['specifications'],
									'repair_price'=>$val['price'],
									'repair_use'=>$val['use'],
									'repair_goods_id'=>$id1
								);
								$id2=$m->add($arr);
								if(false==$id2){
									echo 0;
									exit;
								}
							}
						}
					}

					$msg_from_name=$this->getOneEmployeName($data['goods_employe_id']);
		            $msg_content=$msg_from_name.'向您提交了一条设施(设备)维修申请单，请注意查收';
		            if(I('msg_tip')){
		                //发送消息
		                $msg_title="设施(设备)维修申请单";
		                $this->sendMsg($_POST['check_person_id'],$data['goods_employe_id'],$msg_title,$msg_content);
		            }

		            if(I('msg_phone')){
		                //发送短信
		                $mobile=I('phone_num');
		                $content=$msg_content."【来自丽丰OA系统】";
		                $mobileids=$mobile.strval(time());
		                $this->sendSMS($mobile,$content,$mobileids);
		            }

					echo "申请成功！";
				}
			}
	}

	public function saveRepairPost(){
			$p=M('Process');
	        $number=$_POST['process_num'];
	        $number=intval(substr($number, 0,10));
	        $result=$p->where("process_num='".$number."'")->find();//已经申请
	        if($result){
	            echo '该申请已保存！';
	        }
	        else{
				$data['goods_number']="A3".time();
				$data['goods_type']="3";
				$data['goods_employe_id']=$_POST['employeId'];
				$data['goods_des']=$_POST['des'];
				$data['goods_tag']=$_POST['tag'];
				/*流程数据*/
				$process["process_num"]=$number;
				$process['process_title']="设施(设备)维修申请单";
				$process['process_begin_id']=$data['goods_employe_id'];
				$process['process_now_id']=$_POST['check_person_id'];
				$process['process_time']=$process["process_num"];
				$process['process_state']=$data['goods_tag'];
				$process['process_type']='Goods';
				$process['process_now_id_if_approval']=0;
				$process['process_parent_id']=0;
				$m=M("process");
				$id=$m->add($process);
				if(false==$id){
					echo 0;
					exit;
				}else{
					$data['goods_process_id']=$id;
					$m=M("goods");
					$id1=$m->add($data);
					if(false==$id1){
						echo 0;
						exit;
					}else{
						$m=M('repair');
						$goodsList=$_POST['data'];
						foreach ($goodsList['rows'] as $key=>$val){
							$arr=array(
								'repair_name'=>$val['name'],
								'repair_count'=>$val['count'],
								'repair_specifications'=>$val['specifications'],
								'repair_price'=>$val['price'],
								'repair_use'=>$val['use'],
								'repair_goods_id'=>$id1
							);
							$id2=$m->add($arr);
							if(false==$id2){
								echo 0;
								exit;
							}
						}
					}
				}
				echo '保存成功！';
			}
		}



}